package com.gill.spring.service.impl;


import com.gill.spring.dao.TestMapper;
import com.gill.spring.pojo.Test;
import com.gill.spring.service.TestService;
import com.gill.spring.util.SpringUtils;
import org.springframework.aop.framework.AopContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @program: my-utils-repo
 * @description:
 * @author: Gill
 * @create: 2021-03-31 17:15
 **/
@Service("testService")
public class TestServiceImpl implements TestService {

    @Autowired
    private TestMapper testMapper;


    @Override
    public void add() {
        Test test = testMapper.selectByPrimaryKey(1);
        test.setNumber(test.getNumber() + 1);
        testMapper.updateByPrimaryKey(test);

//        sub();//直接调用同一类的其他带有AOP注解的方法，默认使用this调用，只有代理类调用才会执行AOP的切入逻辑
        // 为了使得sub方法的调用具有事务功能，需要获取本类的代理类实例，然后执行目标方法
        TestService proxy = (TestService) AopContext.currentProxy();
        proxy.sub();
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void sub() {
        Test test = testMapper.selectByPrimaryKey(1);
        test.setNumber(test.getNumber() - 10);
        testMapper.updateByPrimaryKey(test);
        int a = 1/0;
    }
}
